package com.zblx.lcd;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.zblx.lcd.event.MyEvent;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

public abstract class BaseActivity extends AppCompatActivity {

    protected MyApplication app;
    protected TextToSpeech textToSpeech;
    protected Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApplication) getApplication();
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        setContentView(getLayoutRes());
        mUnbinder = ButterKnife.bind(this);
        EventBus.getDefault().register(this);

    }
    /**
     * 获取布局ID
     */
    protected abstract int getLayoutRes();

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MyEvent event) {
    }

    /**
     * 退出当前Activity时被调用,调用之后Activity就结束了
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        EventBus.getDefault().unregister(this);

        try{
            if(null!=textToSpeech){
                textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
                textToSpeech.shutdown(); // 关闭，释放资源
            }
        }catch (Exception e){}
    }

    /**
     * 播放文字语音
     * @param speechText 播放文字
     */
    @SuppressLint("NewApi")
    protected void speechText(final String speechText) {
        try {
            if (textToSpeech != null) {
                textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
                textToSpeech.shutdown(); // 关闭，释放资源
            }

            textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    Log.d("xxx",""+status);
                    if(status==TextToSpeech.SUCCESS){
                        int result = textToSpeech.setLanguage(Locale.CHINA);
                        if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) {
                            Toast.makeText(getApplicationContext(), "暂不支持该中文语言", Toast.LENGTH_SHORT).show();
                        }
                    }
                    textToSpeech.setPitch(1.0f);//方法用来控制音调
                    textToSpeech.setSpeechRate(1.0f);//用来控制语速

                    textToSpeech.speak(speechText,TextToSpeech.QUEUE_FLUSH,null,"UniqueID");
                }
            });
        } catch (Exception e) {

        }
    }




}
